package main.orga;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import main.util.JdbcUtil;

public class AddOrgaIdAndCode {
//	static String old_rootId    = "71011951";
//	static String newOrgaId     = "000300260105";
//	static String newOrgaCode   = "001026105";
//	static String superOrgaCode = "001026";

	static JdbcUtil jdbcUtil;
	public static void main(String[] args) {
		/*
		 * 连接本地数据库：ngmtt_kfrh_gx
		 * 
		 * 0、查询提供的数据是否与历史数据有重复
		 * SELECT * FROM t_kfrh_orgainfo_gx_zhuijia WHERE old_orgaId in(SELECT old_orgaId FROM t_kfrh_orgainfo_gx);
		 * 如果查询结果不为0，说明提供的数据中有重复数据。
		 * 
		 * 1、查询追加数据的根节点list
		 * SELECT * FROM t_kfrh_orgainfo_gx_zhuijia WHERE old_superorgaId NOT IN ( SELECT old_orgaId FROM t_kfrh_orgainfo_gx_zhuijia );
		 * 
		 * 2、查询根节点list的组织机构映射关系
		 * SELECT new_orgacode,new_orgacode FROM t_kfrh_orgainfo_gx WHERE old_orgaId IN ( SELECT old_orgaId FROM t_kfrh_orgainfo_gx_zhuijia WHERE old_superorgaId NOT IN ( SELECT old_orgaId FROM t_kfrh_orgainfo_gx_zhuijia ));
		 * 
		 * 当两条查询语句查出的条数不一致时，说明提供的数据有问题。
		 */
		jdbcUtil = new JdbcUtil();
		String sql = "SELECT a.old_orgaId,a.old_superorgaId,b.new_orgaId AS `new_superorgaId`,b.new_orgacode AS `new_superorgacode` FROM t_kfrh_orgainfo_gx_zhuijia a LEFT JOIN t_kfrh_orgainfo_gx b ON a.old_superorgaId=b.old_orgaId WHERE a.old_superorgaId NOT IN ( SELECT old_orgaId FROM t_kfrh_orgainfo_gx_zhuijia )";
		List<Map<String, Object>> rootInfoList = jdbcUtil.queryForList(sql);
		if (rootInfoList.size() <= 0) {
			System.out.println("根节点信息有误！");
			return;
		}
		List<Map<String, Object>> childNumList = new ArrayList<Map<String,Object>>();
		for (Map<String, Object> rootMap : rootInfoList) {
			String old_rootId    = rootMap.get("old_orgaId").toString();
			String superOrgaCode = rootMap.get("new_superorgacode").toString();
			String newOrgaId     = "";
			String newOrgaCode   = "";
			
			jdbcUtil = new JdbcUtil();
			String queryOrgaid = "SELECT new_orgaId,new_orgacode FROM t_kfrh_orgainfo_gx where new_superorgacode='001026105' ORDER BY new_orgaId DESC LIMIT 0,1 ";
			List<Map<String, Object>> orgaIdList = jdbcUtil.queryForList(sql);
			
			int orgaNum =1;
			boolean superOrgaFlag = false;
			for (Map<String, Object> childNumMap:childNumList) {
				String superOrgaCodeTemp = childNumMap.get("superOrgaCode").toString();
				int childNum = (Integer)childNumMap.get("superOrgaCode");
				if (superOrgaCodeTemp.equals(superOrgaCode)) {
					orgaNum = childNum+1;
					childNumMap.put("childNum", orgaNum);
					superOrgaFlag = true;
					break;
				}
			}
			
			if (!superOrgaFlag) {
				Map<String, Object> tempMap = new HashMap<String, Object>();
				tempMap.put("superOrgaCode", superOrgaCode);
				tempMap.put("childNum", 1);
				childNumList.add(tempMap);
			}
			
			String orgaIdPostfix = "0000".substring(0, 4-(String.valueOf(orgaNum)).length()) + String.valueOf(orgaNum);
			String orgaCodePostfix = "000".substring(0, 3-(String.valueOf(orgaNum)).length()) + String.valueOf(orgaNum);
			
			newOrgaId = rootMap.get("new_superorgaid").toString()+orgaIdPostfix;
			newOrgaCode = superOrgaCode+orgaCodePostfix;
			
			initOrgaInfo(old_rootId,newOrgaId,newOrgaCode,superOrgaCode);
		}
		
	}

	private static void initOrgaInfo(String old_rootId2, String newOrgaId2,
			String newOrgaCode2, String superOrgaCode2) {
		String sql = "update t_kfrh_orgainfo_gx_zhuijia set new_orgaId='"+newOrgaId2
				+"', new_orgacode='"+newOrgaCode2
				+"', new_superorgacode='"+superOrgaCode2
				+"' where old_orgaId='"+old_rootId2+"';";
		System.out.println(sql);
		jdbcUtil = new JdbcUtil();
		jdbcUtil.update(sql);
		
		sql = "SELECT a.old_orgaId FROM t_kfrh_orgainfo_gx_zhuijia a WHERE a.old_superorgaId = '"+old_rootId2+"'";
		System.out.println(sql);
		jdbcUtil = new JdbcUtil();
		List<Map<String, Object>> childList = jdbcUtil.queryForList(sql);
		for (int i = 1; i <= childList.size(); i++) {
			String temp = ""+i;
			String old_rootId3 = (String)childList.get(i-1).get("old_orgaId");
			String newOrgaId3 = newOrgaId2+"0000".substring(0,4-temp.length())+temp;
			String superOrgaCode3 = newOrgaCode2;
			String newOrgaCode3 = newOrgaCode2+"000".substring(0,3-temp.length())+temp;
			initOrgaInfo(old_rootId3, newOrgaId3, newOrgaCode3, superOrgaCode3);
		}
		
	}
}
